<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="70px" class="queryForms">
			<el-form-item label="设备类型" prop="deviceType">
				<el-input v-model="queryParams.deviceType" placeholder="请输入设备类型" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit  />
			</el-form-item>
			<el-form-item label="状态" prop="state">
				<el-select v-model="queryParams.state" placeholder="请选择">
					<el-option v-for="dict in dict.type.default_status" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['erp:deviceType:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['erp:deviceType:remove']">删除</el-button>
			</el-col>
			<!-- <el-col :span="1.5">
				<DocumentShow menuName="equipment/produce/deviceType" />
			</el-col> -->
			<right-toolbar menuNames="deviceType"  :showSearch.sync="showSearch" @changeTableData="changeTableData" @queryTable="getList" :columns.sync="columns" perm="equipment:produce:deviceType:index"></right-toolbar>
		</el-row>

		<div :class="sidebar.opened && eleTabTop <= 85 ? 'eleTab' : !sidebar.opened && eleTabTop <= 85 ? 'eleTabs' : ''" class="informationTab" ref="eleTab">
			<el-table ref="table" @row-click="row_click" v-loading="loading" :data="deviceTypeList" @selection-change="handleSelectionChange" border height="100%">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="序号" align="center" type="index" width="55" />
				<el-table-column label="设备类型" align="center" prop="deviceType" min-width="150" :show-overflow-tooltip="true" v-if="columns[0].visible" />
				<el-table-column label="状态" align="center" prop="state" min-width="150" :show-overflow-tooltip="true" v-if="columns[1].visible">
					<template slot-scope="scope">
						<el-switch v-model="scope.row.state" active-value="1" inactive-value="0" @change="changeStauts(scope.row)"> </el-switch>
					</template>
				</el-table-column>
				<el-table-column label="操作" class-name="small-padding fixed-width" fixed="right" width="200" align="center">
					<template slot-scope="scope">
						<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['erp:deviceType:edit']">修改</el-button>
						<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['erp:deviceType:remove']">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<pagination   menuNames="deviceTypePagination" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改设备类型对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="650px" append-to-body v-dialogDrag>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="设备类型" prop="deviceType">
					<el-input v-model="form.deviceType" placeholder="请输入设备类型" maxlength="20" show-word-limit  />
				</el-form-item>
				<el-form-item label="状态" prop="state">
					<el-radio v-for="dict in dict.type.default_status" :key="dict.value" v-model="form.state" :label="dict.value">{{ dict.label }}</el-radio>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { listDeviceType, getDeviceType, delDeviceType, addDeviceType, updateDeviceType } from "@/api/equipment/produce/deviceType"
import DocumentShow from "@/views/components/DocumentShow"
import { mapGetters } from "vuex"
import { getcolumn } from "@/api/selectList"
export default {
	name: "DeviceType",
	dicts: ["default_status"],
	components: { DocumentShow },
	computed: {
		...mapGetters(["sidebar"])
	},
	data() {
		return {
			fileVedioList: [],
			showfileListDialog: false,
			showTodoListDialog: false,
			eleTabTop: 0, //表格容器距浏览器顶部距离
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 设备类型表格数据
			deviceTypeList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				deviceType: undefined,
				state: undefined
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
				deviceType: [{ required: true, message: "请输入设备类型", trigger: "blur" }]
			},
			// 列信息
			columns: [
				{ key: 0, label: `设备类型`, visible: true },
				{ key: 1, label: `状态`, visible: true }
			]
		}
	},
	created() {
		this.getSize()
	},
	mounted() {
		// 滚动条的获取
		this.handleScrollx()
		window.addEventListener("scroll", this.handleScrollx, true)
	},
	activated() {
		this.getSize()
		//获取表格容器距页面头部的距离
		this.handleScrollx()
	},
	destroyed() {
		window.removeEventListener("scroll", this.handleScrollx, true)
	},
	methods: {
     getSize() {
			getcolumn({ pageName: "deviceTypePagination" }).then(response => {
				this.queryParams.pageSize = response.data.length ? (response.data[0].limit ? response.data[0].limit : 10) : 10
				this.getList()
			})
		},
		changeTableData() {
			this.$nextTick(() => {
				this.$refs.table.doLayout()
			})
		},
		// 单击表格行
		row_click(row, column, event) {
			if (column && column.label == "操作") {
				return
			} else {
				this.$refs.table.toggleRowSelection(row)
			}
		},
		//获取表格容器距页面头部的距离
		handleScrollx() {
			this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top
		},
		/** 查询设备类型列表 */
		getList() {
			this.loading = true
      console.log(this.queryParams)
			listDeviceType(this.queryParams).then(response => {
				this.deviceTypeList = response.rows
				this.total = response.total
				this.loading = false
			})
		},
		// 取消按钮
		cancel() {
			this.open = false
			this.reset()
		},
		// 表单重置
		reset() {
			this.form = {
				deviceType: undefined,
				state: "1"
			}
			this.resetForm("form")
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1
			this.getList()
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm")
			this.handleQuery()
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset()
			this.open = true
			this.title = "添加设备类型"
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset()
			const id = row.id || this.ids
			getDeviceType(id).then(response => {
				this.form = response.data
				this.open = true
				this.title = "修改设备类型"
			})
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.id != null) {
						updateDeviceType(this.form).then(response => {
							this.$modal.msgSuccess("修改成功")
							this.open = false
							this.getList()
						})
					} else {
						addDeviceType(this.form).then(response => {
							this.$modal.msgSuccess("新增成功")
							this.open = false
							this.getList()
						})
					}
				}
			})
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids
			this.$modal
				.confirm("是否确认删除数据项？")
				.then(function () {
					return delDeviceType(ids)
				})
				.then(() => {
					this.getList()
					this.$modal.msgSuccess("删除成功")
				})
				.catch(() => {})
		},
		// 更改状态
		changeStauts(row) {
			let data = {
				id: row.id,
				state: Number(row.state)
			}
			this.$modal
				.confirm("确认要修改状态吗？")
				.then(function () {
					return updateDeviceType(data)
				})
				.then(() => {
					this.$modal.msgSuccess("操作成功")
				})
				.catch(() => {
					row.state = row.state == "0" ? "1" : "0"
				})
		}
		/** 导出按钮操作 */
		// handleExport() {
		//   this.download('erp/deviceType/export', {
		//     ...this.queryParams
		//   }, `deviceType_${new Date().getTime()}.xlsx`)
		// }
	}
}
</script>
<style scoped lang="scss">
// ::v-deep .el-table__fixed{
//   height: 100% !important;
// }
// ::v-deep .el-table__fixed-right,.el-table__fixed-body-wrapper{
//   height: 100% !important;
// }
</style>
